{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "cannot import name 'add_newdocs' from 'numpy' (D:\\Progrram Files\\anaconda\\lib\\site-packages\\numpy\\__init__.py)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-2b12950a9a51>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mmatplotlib\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mpyplot\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrcParams\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"font.sans-serif\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"SimHei\"\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;31m#设置字体\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrcParams\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"axes.unicode_minus\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m \u001b[1;31m#该语句解决图像中的“-”负号的乱码问题\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Progrram Files\\anaconda\\lib\\site-packages\\matplotlib\\__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m    124\u001b[0m \u001b[1;31m# cbook must import matplotlib only within function\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    125\u001b[0m \u001b[1;31m# definitions, so it is safe to import from it here.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 126\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[1;33m.\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mcbook\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    127\u001b[0m from matplotlib.cbook import (\n\u001b[0;32m    128\u001b[0m     _backports, mplDeprecation, dedent, get_label, sanitize_sequence)\n",
      "\u001b[1;32mD:\\Progrram Files\\anaconda\\lib\\site-packages\\matplotlib\\cbook\\__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     32\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mweakref\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mref\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mWeakKeyDictionary\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     33\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 34\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     35\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     36\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mmatplotlib\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Progrram Files\\anaconda\\lib\\site-packages\\numpy\\__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m    140\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mloader\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mpackages\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0moptions\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    141\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 142\u001b[1;33m     \u001b[1;32mfrom\u001b[0m \u001b[1;33m.\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0madd_newdocs\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    143\u001b[0m     __all__ = ['add_newdocs',\n\u001b[0;32m    144\u001b[0m                \u001b[1;34m'ModuleDeprecationWarning'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mImportError\u001b[0m: cannot import name 'add_newdocs' from 'numpy' (D:\\Progrram Files\\anaconda\\lib\\site-packages\\numpy\\__init__.py)"
     ]
    }
   ],
   "source": [
    "from matplotlib import pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "plt.rcParams[\"font.sans-serif\"]=[\"SimHei\"] #设置字体\n",
    "plt.rcParams[\"axes.unicode_minus\"]=False #该语句解决图像中的“-”负号的乱码问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'plt' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-41364b40a8cf>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mfig\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfigure\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;31m#添加子图区域\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0ma1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfig\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd_axes\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;31m#准备数据\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m11\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'plt' is not defined"
     ]
    }
   ],
   "source": [
    "fig = plt.figure()\n",
    "#添加子图区域\n",
    "a1 = fig.add_axes([0,0,1,1])\n",
    "#准备数据\n",
    "x = np.arange(1,11)\n",
    "#绘制指数函数\n",
    "a1.plot(x,np.exp(x))\n",
    "a1.set_ylabel('exp')\n",
    "#添加双轴\n",
    "a2 = a1.twinx()\n",
    "#‘ro’表示红色圆点\n",
    "a2.plot(x, np.log(x),'ro-')\n",
    "#绘制对数函数\n",
    "a2.set_ylabel('log')\n",
    "#绘制图例\n",
    "fig.legend(labels = ('exp','log'),loc='upper left')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure()\n",
    "#添加子图区域，参数值表示[left, bottom, width, height ]\n",
    "ax = fig.add_axes([0,0,1,1])\n",
    "#准备数据\n",
    "langs = ['C', 'C++', 'Java', 'Python', 'PHP']\n",
    "students = [23,17,35,29,12]\n",
    "#绘制柱状图\n",
    "ax.bar(langs,students)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = [[30, 25, 50, 20],\n",
    "[40, 23, 51, 17],\n",
    "[35, 22, 45, 19]]\n",
    "X = np.arange(4)\n",
    "fig = plt.figure()\n",
    "#添加子图区域\n",
    "ax = fig.add_axes([0,0,1,1])\n",
    "#绘制柱状图\n",
    "ax.bar(X + 0.00, data[0], color = 'b', width = 0.25)\n",
    "ax.bar(X + 0.25, data[1], color = 'g', width = 0.25)\n",
    "ax.bar(X + 0.50, data[2], color = 'r', width = 0.25)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "countries = ['日照市', '岚山区'] \n",
    "一类 = np.array([49,56])\n",
    "二类 = np.array([9,13])\n",
    "三类 = np.array([27,39]) \n",
    "四类 = np.array([25,52]) \n",
    "\n",
    "# 此处的 _ 下划线表示将循环取到的值放弃，只得到[0,1,2,3,4]\n",
    "ind = [x for x, _ in enumerate(countries)] \n",
    "#绘制堆叠图\n",
    "plt.bar(ind, 一类, width=0.5, label='一类', color='red') \n",
    "plt.bar(ind, 二类, width=0.5, label='二类', color='orange')\n",
    "plt.bar(ind, 三类, width=0.5, label='三类', color='yellow') \n",
    "plt.bar(ind, 四类, width=0.5, label='四类', color='blue') \n",
    "#设置坐标轴\n",
    "plt.xticks(ind, countries) \n",
    "plt.ylabel(\"数量\") \n",
    "plt.xlabel(\"区县\") \n",
    "plt.legend(loc=\"upper right\") \n",
    "plt.title(\"危化品企业分区统计图\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig,ax = plt.subplots(1,1)\n",
    "a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27])\n",
    "#绘制直方图\n",
    "ax.hist(a, bins = [0,25,50,75,100])\n",
    "#设置坐标轴\n",
    "ax.set_title(\"histogram of result\")\n",
    "ax.set_xticks([0,25,50,75,100])\n",
    "ax.set_xlabel('marks')\n",
    "ax.set_ylabel('no.of students')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "直方图绘制： matplotlib.pyplot.hist()\n",
    "\n",
    "x|必填参数，数组或者数组序列。\n",
    ":-|:-\n",
    "bins|可选参数，整数或者序列，bins 表示每一个间隔的边缘（起点和终点）默认会生成10个间隔。\n",
    "range|指定全局间隔的下限与上限值 (min,max)，元组类型，默认值为 None。\n",
    "density|如果为 True，返回概率密度直方图；默认为 False，返回相应区间元素的个数的直方图。\n",
    "histtype|要绘制的直方图类型，默认值为“bar”，可选值有 barstacked(堆叠条形图)、step(未填充的阶梯图)、stepfilled(已填充的阶梯图)。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure()\n",
    "ax = fig.add_axes([0,0,1,1])\n",
    "#使得X/Y轴的间距相等\n",
    "ax.axis('equal')\n",
    "#准备数据\n",
    "langs = ['C', 'C++', 'Java', 'Python', 'PHP']\n",
    "students = [23,17,35,29,12]\n",
    "#绘制饼状图\n",
    "ax.pie(students, labels = langs,autopct='%1.2f%%')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "X|数组序列，数组元素对应扇形区域的数量大小。\n",
    ":-|:-\n",
    "labels|列表字符串序列，为每个扇形区域备注一个标签名字。\n",
    "color|为每个扇形区域设置颜色，默认按照颜色周期自动设置。\n",
    "autopct|格式化字符串\"fmt%pct\"，使用百分比的格式设置每个扇形\n",
    "区的标签，并将其放置在扇形区内。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = [\"Mon\", \"Tues\", \"Wed\", \"Thur\", \"Fri\",\"Sat\",\"Sun\"]\n",
    "y = [20, 40, 35, 55, 42, 80, 50]\n",
    "# \"g\" 表示红色，marksize用来设置'D'菱形的大小\n",
    "plt.plot(x, y, \"g\", marker='D', markersize=5, label=\"周活\")\n",
    "#绘制坐标轴标签\n",
    "plt.xlabel(\"登录时间\")\n",
    "plt.ylabel(\"用户活跃度\")\n",
    "plt.title(\"C语言中文网活跃度\")\n",
    "#显示图例\n",
    "plt.legend(loc=\"lower right\")\n",
    "#调用 text()在图像上绘制注释文本\n",
    "#x1、y1表示文本所处坐标位置，ha参数控制水平对齐方式, va控制垂直对齐方式，str(y1)表示要绘制的文本\n",
    "for x1, y1 in zip(x, y):\n",
    "    plt.text(x1, y1, str(y1), ha='center', va='bottom', fontsize=10)\n",
    "#保存图片\n",
    "plt.savefig(\"1.jpg\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "girls_grades = [89, 90, 70, 89, 100, 80, 90, 100, 80, 34]\n",
    "boys_grades = [30, 29, 49, 48, 100, 48, 38, 45, 20, 30]\n",
    "grades_range = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]\n",
    "fig=plt.figure()\n",
    "#添加绘图区域\n",
    "ax=fig.add_axes([0,0,1,1])\n",
    "ax.scatter(grades_range, girls_grades, color='r',label=\"girls\")\n",
    "ax.scatter(grades_range, boys_grades, color='b',label=\"boys\")\n",
    "ax.set_xlabel('Grades Range')\n",
    "ax.set_ylabel('Grades Scored')\n",
    "ax.set_title('scatter plot')\n",
    "#添加图例\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#创建xlist、ylist数组\n",
    "xlist = np.linspace(-3.0, 3.0, 100)\n",
    "ylist = np.linspace(-3.0, 3.0, 100)\n",
    "#将上述数据变成网格数据形式\n",
    "X, Y = np.meshgrid(xlist, ylist)\n",
    "#定义Z与X,Y之间的关系\n",
    "Z = np.sqrt(X**2 + Y**2)\n",
    "fig,ax=plt.subplots(1,1)\n",
    "#填充等高线颜色\n",
    "cp = ax.contourf(X, Y, Z)\n",
    "fig.colorbar(cp) # 给图像添加颜色柱\n",
    "ax.set_title('Filled Contours Plot')\n",
    "ax.set_xlabel('x (cm)')\n",
    "ax.set_ylabel('y (cm)')\n",
    "#画等高线\n",
    "plt.contour(X,Y,Z)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x,y = np.meshgrid(np.arange(-2, 2, 0.2), np.arange(-2, 2, 0.25))\n",
    "z = x*np.exp(-x**2 - y**2)\n",
    "#计算数组中元素的梯度\n",
    "v, u = np.gradient(z, 0.2, 0.2)\n",
    "fig, ax = plt.subplots()\n",
    "q = ax.quiver(x,y,u,v)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "参数|说明\n",
    ":-|:-\n",
    "x|一维、二维数组或者序列，表示箭头位置的x坐标。\n",
    "y|一维、二维数组或者序列，表示箭头位置的y坐标。\n",
    "u|一维、二维数组或者序列，表示箭头向量的x分量。\n",
    "v|一维、二维数组或者序列，表示箭头向量的y分量。\n",
    "c|一维、二维数组或者序列，表示箭头颜色。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(10)\n",
    "collectn_1 = np.random.normal(100, 10, 200)\n",
    "collectn_2 = np.random.normal(80, 30, 200)\n",
    "collectn_3 = np.random.normal(90, 20, 200)\n",
    "collectn_4 = np.random.normal(70, 25, 200)\n",
    "data_to_plot=[collectn_1,collectn_2,collectn_3,collectn_4]\n",
    "fig = plt.figure()\n",
    "#创建绘图区域\n",
    "ax = fig.add_axes([0,0,1,1])\n",
    "#创建箱型图\n",
    "bp = ax.boxplot(data_to_plot)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(10)\n",
    "collectn_1 = np.random.normal(100, 10, 200)\n",
    "collectn_2 = np.random.normal(80, 30, 200)\n",
    "collectn_3 = np.random.normal(90, 20, 200)\n",
    "collectn_4 = np.random.normal(70, 25, 200)\n",
    "#创建绘制小提琴图的数据序列\n",
    "data_to_plot = [collectn_1, collectn_2, collectn_3, collectn_4]\n",
    "#创建一个画布\n",
    "fig = plt.figure()\n",
    "#创建一个绘图区域\n",
    "ax = fig.add_axes([0,0,1,1])\n",
    "# 创建一个小提琴图\n",
    "bp = ax.violinplot(data_to_plot)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mpl_toolkits import mplot3d\n",
    "fig = plt.figure()\n",
    "#从三个维度构建\n",
    "z = np.linspace(0, 1, 100)\n",
    "x = z * np.sin(20 * z)\n",
    "y = z * np.cos(20 * z)\n",
    "#调用 ax.plot3D创建三维线图\n",
    "ax = mplot3d.Axes3D(fig)\n",
    "ax.set_title('3D line plot')\n",
    "ax.plot(x,y,z)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure()\n",
    "#创建绘图区域\n",
    "ax = plt.axes(projection='3d')\n",
    "#构建xyz\n",
    "z = np.linspace(0, 1, 100)\n",
    "x = z * np.sin(20 * z)\n",
    "y = z * np.cos(20 * z)\n",
    "c = x + y\n",
    "ax.scatter3D(x, y, z, c=c)\n",
    "ax.set_title('3d Scatter plot')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x, y):\n",
    "   return np.sin(np.sqrt(x ** 2 + y ** 2))\n",
    "#构建x、y数据\n",
    "x = np.linspace(-6, 6, 30)\n",
    "y = np.linspace(-6, 6, 30)\n",
    "#将数据网格化处理\n",
    "X, Y = np.meshgrid(x, y)\n",
    "Z = f(X, Y)\n",
    "fig = plt.figure()\n",
    "ax = plt.axes(projection='3d')\n",
    "#50表示在z轴方向等高线的高度层级，binary颜色从白色变成黑色\n",
    "ax.contour3D(X, Y, Z, 50, cmap='binary')\n",
    "ax.set_xlabel('x')\n",
    "ax.set_ylabel('y')\n",
    "ax.set_zlabel('z')\n",
    "ax.set_title('3D contour')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#要绘制函数图像\n",
    "def f(x, y):\n",
    "   return np.sin(np.sqrt(x ** 2 + y ** 2))\n",
    "#准备x,y数据\n",
    "x = np.linspace(-6, 6, 30)\n",
    "y = np.linspace(-6, 6, 30)\n",
    "#生成x、y网格化数据\n",
    "X, Y = np.meshgrid(x, y)\n",
    "#准备z值\n",
    "Z = f(X, Y)\n",
    "#绘制图像\n",
    "fig = plt.figure()\n",
    "ax = plt.axes(projection='3d')\n",
    "#调用绘制线框图的函数plot_wireframe()\n",
    "ax.plot_wireframe(X, Y, Z, color='black')\n",
    "ax.set_title('wireframe')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#求向量积(outer()方法又称外积)\n",
    "x = np.outer(np.linspace(-2, 2, 30), np.ones(30))\n",
    "#矩阵转置\n",
    "y = x.copy().T \n",
    "#数据z\n",
    "z = np.cos(x ** 2 + y ** 2)\n",
    "#绘制曲面图\n",
    "fig = plt.figure()\n",
    "ax = plt.axes(projection='3d')\n",
    "#调用plot_surface()函数\n",
    "ax.plot_surface(x, y, z,cmap='viridis', edgecolor='none')\n",
    "ax.set_title('Surface plot')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Matplotlib 支持广泛的文本格式，比如 TTF 页面语言、Unicode 字符等。这是因为 Matplotlib 内置了 matplotlib.font_manager 字体管理器，它是一个实现了跨平台，并符合 W3C 字体查找算法的字体集合。\n",
    "TTF（TrueType Font） 是苹果公司和微软公司合作开发的页面描述语言，用来描述字符的轮廓，结合了光栅技术和矢量技术的优点。\n",
    "\n",
    "用户可以对文本属性进行控制，比如字体大小、粗细、位置和颜色等。\n",
    "\n",
    "与此同时，Matplotlib 也支持绘制 TeX 包含的数学符号。TeX 是一套功能强大、十分灵活的排版语言，它可以用来绘制文本、符号、数学表达式等。通过下表中的方法可以绘制出相应的内容：\n",
    "\n",
    "text|在绘图区域的任意位置添加文本。\n",
    ":-|:-\n",
    "annotate|在绘图区域的任意位置添加带有可选箭头的注释。\n",
    "xlabel|在绘图区域的 x 轴上添加标签。\n",
    "ylabel|在绘图区域的 y 轴上添加标签。\n",
    "title|为绘图区域添加标题。\n",
    "figtext|在画布的任意位置添加文本。\n",
    "suptitle|为画布中添加标题。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure()\n",
    "#添加绘图区域\n",
    "ax = fig.add_axes([0,0,1,1])\n",
    "#设置格式\n",
    "ax.set_title('axes title')\n",
    "ax.set_xlabel('xlabel')\n",
    "ax.set_ylabel('ylabel')\n",
    "# 3,8 表示x，y的坐标点；style设置字体样式为斜体；bbox用来设置盒子的属性，比如背景色\n",
    "ax.text(3, 8, 'C语言中网网，编程爱好者都喜欢的网站', style='italic',bbox = {'facecolor': 'yellow'},fontsize=15)\n",
    "#绘制数学表达式,用$符包裹\n",
    "ax.text(2, 6, r'an equation: $E = mc^2$', fontsize = 15)\n",
    "#添加文字，并设置样式\n",
    "ax.text(4, 0.05, '网址：c.biancheng.net',verticalalignment = 'bottom', color = 'green', fontsize = 15)\n",
    "ax.plot([2], [1], 'o')\n",
    "#xy为点的坐标；xytext为注释内容坐标；arrowprops设置箭头的属性\n",
    "ax.annotate('C语言中文网', xy = (2, 1), xytext = (3, 4),arrowprops = dict(facecolor = 'blue', shrink = 0.1))\n",
    "#设置坐标轴x,y\n",
    "ax.axis([0, 10, 0, 10])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t = np.arange(0.0, 2.0, 0.01)\n",
    "s = np.sin(2*np.pi*t)\n",
    "#绘制函数图像\n",
    "plt.plot(t,s)\n",
    "#设置标题\n",
    "plt.title(r'$\\alpha_i> \\beta_i$', fontsize=20)\n",
    "#设置数学表达式\n",
    "plt.text(0.6, 0.6, r'$\\mathcal{A}\\mathrm{sin}(2 \\omega t)$', fontsize = 20)\n",
    "#设置数学表达式\n",
    "plt.text(0.1, -0.5, r'$\\sqrt{2}$', fontsize=10)\n",
    "plt.xlabel('time (s)')\n",
    "plt.ylabel('volts (mV)')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig=plt.figure()\n",
    "ax1=fig.add_subplot(221)\n",
    "ax2=fig.add_subplot(222)\n",
    "ax3=fig.add_subplot(223)\n",
    "ax4=fig.add_subplot(224)\n",
    "#准备数据\n",
    "#绘制z = sqrt(x^2+y^2)图像\n",
    "points=np.arange(-5,5,0.01)\n",
    "# meshgrid 接受两个一维数组，然后产生两个二维矩阵\n",
    "xs,ys=np.meshgrid(points,points)\n",
    "#绘制图像-\n",
    "z=np.sqrt(xs**2+ys**2)\n",
    "ax = fig.add_subplot(221)\n",
    "#默认\n",
    "ax.imshow(z)\n",
    "ax = fig.add_subplot(222)\n",
    "ax.imshow(z,cmap = \"gray\")\n",
    "ax = fig.add_subplot(223)\n",
    "ax.imshow(z,cmap=\"cool\")\n",
    "ax = fig.add_subplot(224)\n",
    "ax.imshow(z,cmap=\"hot\")\n",
    "#显示图像\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
